//
//  UUOrderCell.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 22/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUOrderCell.h"
#import "UUResourceManager.h"
#import <UUFoundation/NSAttributedString+Extensions.h>
#import "UUDriverManager.h"
#import "UUPermissionManager.h"

@interface UUOrderCell ()

@property (nonatomic, weak) IBOutlet UILabel *titleLabel;
@property (nonatomic, weak) IBOutlet UILabel *subtitleLabel;
@property (nonatomic, weak) IBOutlet UILabel *stateLabel;
@property (nonatomic, weak) IBOutlet UILabel *priceLabel;
@property (nonatomic, weak) IBOutlet UILabel *timeLabel;
@property (nonatomic, weak) IBOutlet UILabel *contactLabel;
@property (nonatomic, weak) IBOutlet UIButton *actionButton;

@property (nonatomic, strong) UUOrderModel *orderModel;

@end

@implementation UUOrderCell

+ (CGFloat)preferredHeight {
    return 105;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.subtitleLabel.textColor = COMMON_TEXT_LIGHT_COLOR;
    self.timeLabel.textColor = COMMON_TEXT_LIGHT_COLOR;
    self.priceLabel.textColor = COMMON_TEXT_ORANGE_COLOR;
    
    [self.actionButton addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
    
    if (![GetPermissionManager() checkPermission:UUPermissionFlagOrderReadWrite]) {
        self.actionButton.enabled = NO;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)action {
    if (self.orderModel.status == UUOrderStatusWaitingForConfirm) {
        [UIAlertController presentAlertControllerWithTitle:@"确认接单后不可取消订单，确定？" message:nil doneBlock:^{
            BLOCK_SAFE_CALLS(self.confirmOrderBlock);
        } cancelBlock:nil];
    } else if (self.orderModel.status == UUOrderStatusWaitingForAssign) {
        BLOCK_SAFE_CALLS(self.assignOrderBlock);
    }
        
}

- (void)setupWithModel:(UUOrderModel *)orderModel {
    self.orderModel = orderModel;
    
    UIImage *orderTypeImage = nil;
    NSString *subtitle = nil;
    switch (orderModel.type) {
        case UUOrderTypeDay:
            orderTypeImage = [UIImage imageNamed:@"OrderTypeDay"];
            if (floor(orderModel.days) == orderModel.days) {
                subtitle = [NSString stringWithFormat:@"%ld辆 %.0f天", orderModel.carNum, orderModel.days];
            } else {
                subtitle = [NSString stringWithFormat:@"%ld辆 %.1f天", orderModel.carNum, orderModel.days];
            }
            break;
        case UUOrderTypeInstant:
            orderTypeImage = [UIImage imageNamed:@"OrderTypeInstantQuotation"];
            if (floor(orderModel.days) == orderModel.days) {
                subtitle = [NSString stringWithFormat:@"%ld辆 %.0f天", orderModel.carNum, orderModel.days];
            } else {
                subtitle = [NSString stringWithFormat:@"%ld辆 %.1f天", orderModel.carNum, orderModel.days];
            }
            break;
        case UUOrderTypeLine:
            orderTypeImage = [UIImage imageNamed:@"OrderTypeLine"];
            subtitle = [NSString stringWithFormat:@"%ld辆 %@", orderModel.carNum, orderModel.line];
            break;
        default:
            DKAssert(0);
            break;
    }
    
    NSString *categoryName = [GetCarCategoryManager() fetchCarCategoryWithCategoryId:orderModel.carCategoryId].name ?: @"";
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:categoryName];
    [attributedString addImage:orderTypeImage];
    self.titleLabel.attributedText = attributedString;
    
    NSString *timeString = [orderModel.departTime formatDateByFormatString:@"MM-dd HH:mm"];
    
    UUCityModel *city = [GetCityManager() fetchCityWithCityId:orderModel.departCityId];
    if (city) {
        self.timeLabel.text = [NSString stringWithFormat:@"%@ %@", city.simpleName, timeString];
    } else {
        self.timeLabel.text = [NSString stringWithFormat:@"%@", timeString];
    }
    
    self.subtitleLabel.text = subtitle;
    
    if (orderModel.refundStatus == UUOrderRefundStatusInRefunding) {
        switch (orderModel.refundStatus) {
            case UUOrderRefundStatusInRefunding:
                self.stateLabel.hidden = NO;
                self.priceLabel.hidden = YES;
                self.contactLabel.hidden = YES;
                self.actionButton.hidden = YES;
                
                self.stateLabel.textColor = COMMON_TEXT_RED_COLOR;
                self.stateLabel.text = @"退款中";
                break;
            default:
                DKAssert(0);
                break;
        }
    } else {
        switch (orderModel.status) {
            case UUOrderStatusWaitingForService: {
                self.stateLabel.hidden = YES;
                self.priceLabel.hidden = YES;
                self.actionButton.hidden = YES;
                self.contactLabel.hidden = NO;
                
                UUDriverModel *driver = [GetDriverManager() fetchDriverWithDriverId:orderModel.dispatch.firstObject.driverId];
                if (driver) {
                    self.contactLabel.text = [NSString stringWithFormat:@"%@ %@", driver.name, driver.mobile];
                }
            }
                break;
            case UUOrderStatusWaitingForConfirm: {
                self.stateLabel.hidden = YES;
                self.priceLabel.hidden = NO;
                self.contactLabel.hidden = YES;
                self.actionButton.hidden = NO;
                
                NSMutableAttributedString *priceAttributedString = [[NSMutableAttributedString alloc] initWithString:@"￥"];
                [priceAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:[orderModel.vendorMoney ?: @(0) toCurrencyWithoutCurrencySymbol]
                                                                                              attributes:@{
                                                                                                           NSFontAttributeName : [UIFont systemFontOfSize:19]
                                                                                                           }]];
                self.priceLabel.attributedText = priceAttributedString;
                [self.actionButton setTitle:@"确认" forState:UIControlStateNormal];
                [self.actionButton setBackgroundImage:GetResourceManager().buttonImage1 forState:UIControlStateNormal];
                [self.actionButton setBackgroundImage:GetResourceManager().buttonImage1HL forState:UIControlStateHighlighted];
            }
                break;
            case UUOrderStatusWaitingForAssign:
                self.stateLabel.hidden = NO;
                self.priceLabel.hidden = YES;
                self.actionButton.hidden = NO;
                self.contactLabel.hidden = YES;
                
                [self.actionButton setTitle:@"分配" forState:UIControlStateNormal];
                [self.actionButton setBackgroundImage:GetResourceManager().buttonImage2 forState:UIControlStateNormal];
                [self.actionButton setBackgroundImage:GetResourceManager().buttonImage2HL forState:UIControlStateHighlighted];
                self.stateLabel.textColor = COMMON_TEXT_RED_COLOR;
                self.stateLabel.text = @"未分配";
                break;
            case UUOrderStatusInService: {
                self.stateLabel.hidden = YES;
                self.priceLabel.hidden = YES;
                self.actionButton.hidden = YES;
                self.contactLabel.hidden = NO;
                
                UUDriverModel *driver = [GetDriverManager() fetchDriverWithDriverId:orderModel.dispatch.firstObject.driverId];
                if (driver) {
                    self.contactLabel.text = [NSString stringWithFormat:@"%@ %@", driver.name, driver.mobile];
                }
            }
                break;
            case UUOrderStatusCancelled:
            case UUOrderStatusWaitingForPay:
                self.stateLabel.hidden = NO;
                self.priceLabel.hidden = YES;
                self.contactLabel.hidden = YES;
                self.actionButton.hidden = YES;
                
                self.stateLabel.textColor = COMMON_TEXT_LIGHT_COLOR;
                
                if (orderModel.status == UUOrderStatusWaitingForPay) {
                    self.stateLabel.text = @"待支付";
                } else {
                    self.stateLabel.text = @"已取消";
                }

                break;
            case UUOrderStatusComplete:
                self.stateLabel.hidden = NO;
                self.priceLabel.hidden = YES;
                self.contactLabel.hidden = YES;
                self.actionButton.hidden = YES;
                
                self.stateLabel.textColor = COMMON_TEXT_LIGHT_COLOR;
                self.stateLabel.text = @"已完成";
                break;
            case UUOrderStatusRefunded:
                self.stateLabel.hidden = NO;
                self.priceLabel.hidden = YES;
                self.contactLabel.hidden = YES;
                self.actionButton.hidden = YES;
                
                self.stateLabel.textColor = COMMON_TEXT_RED_COLOR;
                self.stateLabel.text = @"已退款";
                break;
            default:
                DKAssert(0);
                break;
        }
    }
}

@end
